package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProducePackageRuleRpcService;
import com.hunttown.mes.rpc.domain.MesProducePackageRuleDTO;
import com.hunttown.mes.rpc.domain.query.MesProducePackageRuleDTOQuery;
import com.hunttown.mes.service.MesProducePackageRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProducePackageRuleConverts.convert;

/**
 * created by wangjunfu on 2022-06-07 08:15:42
 */
@Service
public class MesProducePackageRuleRpcServiceImpl implements MesProducePackageRuleRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProducePackageRuleRpcServiceImpl.class);

    private final MesProducePackageRuleService mesProducePackageRuleService;

    @Autowired
    public MesProducePackageRuleRpcServiceImpl(MesProducePackageRuleService mesProducePackageRuleService) {
        this.mesProducePackageRuleService = mesProducePackageRuleService;
    }

    @Override
    public MesProducePackageRuleDTO insert(MesProducePackageRuleDTO mesProducePackageRuleDTO) {
        try {
            return convert(mesProducePackageRuleService.insert(convert(mesProducePackageRuleDTO)));
        } catch (Exception e) {
            logger.error("MesProducePackageRule create failed.", e);
            throw new RuntimeException("MesProducePackageRule create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProducePackageRuleDTO mesProducePackageRuleDTO) {
        try {
            return mesProducePackageRuleService.updateInfoById(convert(mesProducePackageRuleDTO));
        } catch (Exception e) {
            logger.error("MesProducePackageRule update by id failed.", e);
            throw new RuntimeException("MesProducePackageRule update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProducePackageRuleDTOQuery mesProducePackageRuleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProducePackageRuleDTOQuery.getW_id() != null && mesProducePackageRuleDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProducePackageRuleDTOQuery.getW_id());
            }

            if (mesProducePackageRuleDTOQuery.getIds() != null && !mesProducePackageRuleDTOQuery.getIds().equals("")) {
                query.put("ids", mesProducePackageRuleDTOQuery.getIds());
            }

            if (mesProducePackageRuleDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProducePackageRuleDTOQuery.getStopFlag());
            }

            if (mesProducePackageRuleDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProducePackageRuleDTOQuery.getDeleteFlag());
            }

            return mesProducePackageRuleService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProducePackageRule update by query failed.", e);
            throw new RuntimeException("MesProducePackageRule update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProducePackageRuleDTO selectById(Integer id) {
        try {
            return convert(mesProducePackageRuleService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProducePackageRule select by id failed.", e);
            throw new RuntimeException("MesProducePackageRule select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProducePackageRuleDTO selectObjByQuery(MesProducePackageRuleDTOQuery mesProducePackageRuleDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProducePackageRuleDTOQuery.getProduceId() != null) {
                query.put("produceId", mesProducePackageRuleDTOQuery.getProduceId());
            }

            if (mesProducePackageRuleDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProducePackageRuleDTOQuery.getStopFlag());
            }

            if (mesProducePackageRuleDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProducePackageRuleDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProducePackageRuleDTOQuery.getOrderBy());
            }

            return convert(mesProducePackageRuleService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProducePackageRule select obj by query failed.", e);
            throw new RuntimeException("MesProducePackageRule select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProducePackageRuleDTO> getListForPage(MesProducePackageRuleDTOQuery mesProducePackageRuleDTOQuery) {
        Query query = Query.page(mesProducePackageRuleDTOQuery.getPn(), mesProducePackageRuleDTOQuery.getPageSize());

        if (mesProducePackageRuleDTOQuery.getId() != null && mesProducePackageRuleDTOQuery.getId() > 0) {
            query.put("id", mesProducePackageRuleDTOQuery.getId());
        }

        if (mesProducePackageRuleDTOQuery.getIds() != null && !mesProducePackageRuleDTOQuery.getIds().equals("")) {
            query.put("ids", mesProducePackageRuleDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getLikeName())) {
            query.put("likeName", mesProducePackageRuleDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getShowDate())) {
            query.put("showDate", mesProducePackageRuleDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getStartDate())) {
            query.put("startDate", mesProducePackageRuleDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getEndDate())) {
            query.put("endDate", mesProducePackageRuleDTOQuery.getEndDate());
        }

        if (mesProducePackageRuleDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProducePackageRuleDTOQuery.getStopFlag());
        }

        if (mesProducePackageRuleDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProducePackageRuleDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProducePackageRuleDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProducePackageRuleDTOQuery.getOrderBy());
        }

        List<MesProducePackageRuleDTO> list = convert(mesProducePackageRuleService.selectByQuery(query));
        Page<MesProducePackageRuleDTO> page = Page.from(mesProducePackageRuleDTOQuery.getPn(), mesProducePackageRuleDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProducePackageRuleService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProducePackageRuleService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProducePackageRule delete failed.", e);
            throw new RuntimeException("MesProducePackageRule delete failed, Please order your code.", e);
        }
    }
}